"""
created on 2023-03-12
author: zjh
description： collect Hadoop jmx metrics
"""
import threading

import requests
import wx

import prometheus_client
from prometheus_client import Counter, Gauge
from prometheus_client.core import CollectorRegistry
from flask import Response, Flask
from psutil import virtual_memory
from psutil import cpu_times
import json
import urllib.request

#获取active状态的Resourcemanager节点
class clusterBaseinfo:
    def getactivenode(self,list_ip):
        for ip in list_ip:
            rmurl = str('http://') + ip + str('/jmx?qry=Hadoop:service=NameNode,name=NameNodeStatus')
            try:
                html = requests.get(rmurl)
                hjson = json.loads(html.text)
                if hjson['beans'][0]['State'] == 'active':
                    #print(hjson["clusterInfo"])
                    #print(ip + ' is active')
                    return ip
                else:
                 isStandby.set(0)
            except:

                print(ip + " is standby")




app = Flask(__name__)

REGISTRY = CollectorRegistry(auto_describe=False)
count = Counter(
    'count',
    'count',
    registry=REGISTRY
)
MemNonHeapUsedM = Gauge(
 'MemNonHeapUsedM',
 'MemNonHeapUsedM',
 registry=REGISTRY
)
MemNonHeapCommittedM = Gauge(
 'MemNonHeapCommittedM',
 'MemNonHeapCommittedM',
 registry=REGISTRY
)
MemNonHeapMaxM = Gauge(
 'MemNonHeapMaxM',
 'MemNonHeapMaxM',
 registry=REGISTRY
)
MemHeapUsedM = Gauge(
 'MemHeapUsedM',
 'MemHeapUsedM',
 registry=REGISTRY
)
MemHeapCommittedM = Gauge(
 'MemHeapCommittedM',
 'MemHeapCommittedM',
 registry=REGISTRY
)
MemHeapMaxM = Gauge(
 'MemHeapMaxM',
 'MemHeapMaxM',
 registry=REGISTRY
)
MemMaxM = Gauge(
 'MemMaxM',
 'MemMaxM',
 registry=REGISTRY
)
GcCount = Gauge(
 'GcCount',
 'GcCount',
 registry=REGISTRY
)
GcTimeMillis = Gauge(
 'GcTimeMillis',
 'GcTimeMillis',
 registry=REGISTRY
)
GcNumWarnThresholdExceeded = Gauge(
 'GcNumWarnThresholdExceeded',
 'GcNumWarnThresholdExceeded',
 registry=REGISTRY
)
GcNumInfoThresholdExceeded = Gauge(
 'GcNumInfoThresholdExceeded',
 'GcNumInfoThresholdExceeded',
 registry=REGISTRY
)
GcTotalExtraSleepTime = Gauge(
 'GcTotalExtraSleepTime',
 'GcTotalExtraSleepTime',
 registry=REGISTRY
)
ThreadsNew = Gauge(
 'ThreadsNew',
 'ThreadsNew',
 registry=REGISTRY
)
ThreadsRunnable = Gauge(
 'ThreadsRunnable',
 'ThreadsRunnable',
 registry=REGISTRY
)
ThreadsBlocked = Gauge(
 'ThreadsBlocked',
 'ThreadsBlocked',
 registry=REGISTRY
)
ThreadsWaiting = Gauge(
 'ThreadsWaiting',
 'ThreadsWaiting',
 registry=REGISTRY
)
ThreadsTimedWaiting = Gauge(
 'ThreadsTimedWaiting',
 'ThreadsTimedWaiting',
 registry=REGISTRY
)
ThreadsTerminated = Gauge(
 'ThreadsTerminated',
 'ThreadsTerminated',
 registry=REGISTRY
)
LogFatal = Gauge(
 'LogFatal',
 'LogFatal',
 registry=REGISTRY
)
LogError = Gauge(
 'LogError',
 'LogError',
 registry=REGISTRY
)
LogWarn = Gauge(
 'LogWarn',
 'LogWarn',
 registry=REGISTRY
)
LogInfo = Gauge(
 'LogInfo',
 'LogInfo',
 registry=REGISTRY
)
Uptime = Gauge(
 'Uptime',
 'Uptime',
 registry=REGISTRY
)
StartTime = Gauge(
 'StartTime',
 'StartTime',
 registry=REGISTRY
)
ElapsedTime = Gauge(
 'ElapsedTime',
 'ElapsedTime',
 registry=REGISTRY
)
PercentComplete = Gauge(
 'PercentComplete',
 'PercentComplete',
 registry=REGISTRY
)
LoadingFsImageCount = Gauge(
 'LoadingFsImageCount',
 'LoadingFsImageCount',
 registry=REGISTRY
)
LoadingFsImageElapsedTime = Gauge(
 'LoadingFsImageElapsedTime',
 'LoadingFsImageElapsedTime',
 registry=REGISTRY
)
LoadingFsImageTotal = Gauge(
 'LoadingFsImageTotal',
 'LoadingFsImageTotal',
 registry=REGISTRY
)
LoadingFsImagePercentComplete = Gauge(
 'LoadingFsImagePercentComplete',
 'LoadingFsImagePercentComplete',
 registry=REGISTRY
)
LoadingEditsCount = Gauge(
 'LoadingEditsCount',
 'LoadingEditsCount',
 registry=REGISTRY
)
LoadingEditsElapsedTime = Gauge(
 'LoadingEditsElapsedTime',
 'LoadingEditsElapsedTime',
 registry=REGISTRY
)
LoadingEditsTotal = Gauge(
 'LoadingEditsTotal',
 'LoadingEditsTotal',
 registry=REGISTRY
)
LoadingEditsPercentComplete = Gauge(
 'LoadingEditsPercentComplete',
 'LoadingEditsPercentComplete',
 registry=REGISTRY
)
SavingCheckpointCount = Gauge(
 'SavingCheckpointCount',
 'SavingCheckpointCount',
 registry=REGISTRY
)
SavingCheckpointElapsedTime = Gauge(
 'SavingCheckpointElapsedTime',
 'SavingCheckpointElapsedTime',
 registry=REGISTRY
)
SavingCheckpointTotal = Gauge(
 'SavingCheckpointTotal',
 'SavingCheckpointTotal',
 registry=REGISTRY
)
SavingCheckpointPercentComplete = Gauge(
 'SavingCheckpointPercentComplete',
 'SavingCheckpointPercentComplete',
 registry=REGISTRY
)
SafeModeCount = Gauge(
 'SafeModeCount',
 'SafeModeCount',
 registry=REGISTRY
)
SafeModeElapsedTime = Gauge(
 'SafeModeElapsedTime',
 'SafeModeElapsedTime',
 registry=REGISTRY
)
SafeModeTotal = Gauge(
 'SafeModeTotal',
 'SafeModeTotal',
 registry=REGISTRY
)
SafeModePercentComplete = Gauge(
 'SafeModePercentComplete',
 'SafeModePercentComplete',
 registry=REGISTRY
)
PeakThreadCount = Gauge(
 'PeakThreadCount',
 'PeakThreadCount',
 registry=REGISTRY
)
DaemonThreadCount = Gauge(
 'DaemonThreadCount',
 'DaemonThreadCount',
 registry=REGISTRY
)
TotalStartedThreadCount = Gauge(
 'TotalStartedThreadCount',
 'TotalStartedThreadCount',
 registry=REGISTRY
)
ThreadCpuTimeSupported = Gauge(
 'ThreadCpuTimeSupported',
 'ThreadCpuTimeSupported',
 registry=REGISTRY
)
CurrentThreadCpuTime = Gauge(
 'CurrentThreadCpuTime',
 'CurrentThreadCpuTime',
 registry=REGISTRY
)
CurrentThreadUserTime = Gauge(
 'CurrentThreadUserTime',
 'CurrentThreadUserTime',
 registry=REGISTRY
)
ThreadCount = Gauge(
 'ThreadCount',
 'ThreadCount',
 registry=REGISTRY
)
OpenFileDescriptorCount = Gauge(
 'OpenFileDescriptorCount',
 'OpenFileDescriptorCount',
 registry=REGISTRY
)
MaxFileDescriptorCount = Gauge(
 'MaxFileDescriptorCount',
 'MaxFileDescriptorCount',
 registry=REGISTRY
)
CommittedVirtualMemorySize = Gauge(
 'CommittedVirtualMemorySize',
 'CommittedVirtualMemorySize',
 registry=REGISTRY
)
TotalSwapSpaceSize = Gauge(
 'TotalSwapSpaceSize',
 'TotalSwapSpaceSize',
 registry=REGISTRY
)
FreeSwapSpaceSize = Gauge(
 'FreeSwapSpaceSize',
 'FreeSwapSpaceSize',
 registry=REGISTRY
)
ProcessCpuTime = Gauge(
 'ProcessCpuTime',
 'ProcessCpuTime',
 registry=REGISTRY
)
FreePhysicalMemorySize = Gauge(
 'FreePhysicalMemorySize',
 'FreePhysicalMemorySize',
 registry=REGISTRY
)
TotalPhysicalMemorySize = Gauge(
 'TotalPhysicalMemorySize',
 'TotalPhysicalMemorySize',
 registry=REGISTRY
)
SystemCpuLoad = Gauge(
 'SystemCpuLoad',
 'SystemCpuLoad',
 registry=REGISTRY
)
ProcessCpuLoad = Gauge(
 'ProcessCpuLoad',
 'ProcessCpuLoad',
 registry=REGISTRY
)
AvailableProcessors = Gauge(
 'AvailableProcessors',
 'AvailableProcessors',
 registry=REGISTRY
)
MissingBlocks = Gauge(
 'MissingBlocks',
 'MissingBlocks',
 registry=REGISTRY
)
ExpiredHeartbeats = Gauge(
 'ExpiredHeartbeats',
 'ExpiredHeartbeats',
 registry=REGISTRY
)
TransactionsSinceLastCheckpoint = Gauge(
 'TransactionsSinceLastCheckpoint',
 'TransactionsSinceLastCheckpoint',
 registry=REGISTRY
)
TransactionsSinceLastLogRoll = Gauge(
 'TransactionsSinceLastLogRoll',
 'TransactionsSinceLastLogRoll',
 registry=REGISTRY
)
LastWrittenTransactionId = Gauge(
 'LastWrittenTransactionId',
 'LastWrittenTransactionId',
 registry=REGISTRY
)
LastCheckpointTime = Gauge(
 'LastCheckpointTime',
 'LastCheckpointTime',
 registry=REGISTRY
)
CapacityTotal = Gauge(
 'CapacityTotal',
 'CapacityTotal',
 registry=REGISTRY
)
CapacityTotalGB = Gauge(
 'CapacityTotalGB',
 'CapacityTotalGB',
 registry=REGISTRY
)

CapacityUsedGB = Gauge(
 'CapacityUsedGB',
 'CapacityUsedGB',
 registry=REGISTRY
)

CapacityRemainingGB = Gauge(
 'CapacityRemainingGB',
 'CapacityRemainingGB',
 registry=REGISTRY
)
CapacityUsedNonDFS = Gauge(
 'CapacityUsedNonDFS',
 'CapacityUsedNonDFS',
 registry=REGISTRY
)

Snapshots = Gauge(
 'Snapshots',
 'Snapshots',
 registry=REGISTRY
)

FilesTotal = Gauge(
 'FilesTotal',
 'FilesTotal',
 registry=REGISTRY
)

CorruptBlocks = Gauge(
 'CorruptBlocks',
 'CorruptBlocks',
 registry=REGISTRY
)
ScheduledReplicationBlocks = Gauge(
 'ScheduledReplicationBlocks',
 'ScheduledReplicationBlocks',
 registry=REGISTRY
)
PendingDeletionBlocks = Gauge(
 'PendingDeletionBlocks',
 'PendingDeletionBlocks',
 registry=REGISTRY
)
ExcessBlocks = Gauge(
 'ExcessBlocks',
 'ExcessBlocks',
 registry=REGISTRY
)
PostponedMisreplicatedBlocks = Gauge(
 'PostponedMisreplicatedBlocks',
 'PostponedMisreplicatedBlocks',
 registry=REGISTRY
)
PendingDataNodeMessageCount = Gauge(
 'PendingDataNodeMessageCount',
 'PendingDataNodeMessageCount',
 registry=REGISTRY
)
MillisSinceLastLoadedEdits = Gauge(
 'MillisSinceLastLoadedEdits',
 'MillisSinceLastLoadedEdits',
 registry=REGISTRY
)
BlockCapacity = Gauge(
 'BlockCapacity',
 'BlockCapacity',
 registry=REGISTRY
)
StaleDataNodes = Gauge(
 'StaleDataNodes',
 'StaleDataNodes',
 registry=REGISTRY
)
TotalFiles = Gauge(
 'TotalFiles',
 'TotalFiles',
 registry=REGISTRY
)
VersionRequestNumOps = Gauge(
 'VersionRequestNumOps',
 'VersionRequestNumOps',
 registry=REGISTRY
)
VersionRequestAvgTime = Gauge(
 'VersionRequestAvgTime',
 'VersionRequestAvgTime',
 registry=REGISTRY
)
RegisterDatanodeNumOps = Gauge(
 'RegisterDatanodeNumOps',
 'RegisterDatanodeNumOps',
 registry=REGISTRY
)
RegisterDatanodeAvgTime = Gauge(
 'RegisterDatanodeAvgTime',
 'RegisterDatanodeAvgTime',
 registry=REGISTRY
)
SendHeartbeatNumOps = Gauge(
 'SendHeartbeatNumOps',
 'SendHeartbeatNumOps',
 registry=REGISTRY
)
SendHeartbeatAvgTime = Gauge(
 'SendHeartbeatAvgTime',
 'SendHeartbeatAvgTime',
 registry=REGISTRY
)
BlockReportNumOps = Gauge(
 'BlockReportNumOps',
 'BlockReportNumOps',
 registry=REGISTRY
)

GetServiceStatusNumOps = Gauge(
 'GetServiceStatusNumOps',
 'GetServiceStatusNumOps',
 registry=REGISTRY
)
GetServiceStatusAvgTime = Gauge(
 'GetServiceStatusAvgTime',
 'GetServiceStatusAvgTime',
 registry=REGISTRY
)
MonitorHealthNumOps = Gauge(
 'MonitorHealthNumOps',
 'MonitorHealthNumOps',
 registry=REGISTRY
)
MonitorHealthAvgTime = Gauge(
 'MonitorHealthAvgTime',
 'MonitorHealthAvgTime',
 registry=REGISTRY
)
TransitionToActiveNumOps = Gauge(
 'TransitionToActiveNumOps',
 'TransitionToActiveNumOps',
 registry=REGISTRY
)
TransitionToActiveAvgTime = Gauge(
 'TransitionToActiveAvgTime',
 'TransitionToActiveAvgTime',
 registry=REGISTRY
)
RollEditLogNumOps = Gauge(
 'RollEditLogNumOps',
 'RollEditLogNumOps',
 registry=REGISTRY
)
RollEditLogAvgTime = Gauge(
 'RollEditLogAvgTime',
 'RollEditLogAvgTime',
 registry=REGISTRY
)
TransitionToStandbyNumOps = Gauge(
 'TransitionToStandbyNumOps',
 'TransitionToStandbyNumOps',
 registry=REGISTRY
)
TransitionToStandbyAvgTime = Gauge(
 'TransitionToStandbyAvgTime',
 'TransitionToStandbyAvgTime',
 registry=REGISTRY
)
CreateFileOps = Gauge(
 'CreateFileOps',
 'CreateFileOps',
 registry=REGISTRY
)
FilesCreated = Gauge(
 'FilesCreated',
 'FilesCreated',
 registry=REGISTRY
)
FilesAppended = Gauge(
 'FilesAppended',
 'FilesAppended',
 registry=REGISTRY
)
GetBlockLocations = Gauge(
 'GetBlockLocations',
 'GetBlockLocations',
 registry=REGISTRY
)
FilesRenamed = Gauge(
 'FilesRenamed',
 'FilesRenamed',
 registry=REGISTRY
)
GetListingOps = Gauge(
 'GetListingOps',
 'GetListingOps',
 registry=REGISTRY
)
DeleteFileOps = Gauge(
 'DeleteFileOps',
 'DeleteFileOps',
 registry=REGISTRY
)
FilesDeleted = Gauge(
 'FilesDeleted',
 'FilesDeleted',
 registry=REGISTRY
)
FileInfoOps = Gauge(
 'FileInfoOps',
 'FileInfoOps',
 registry=REGISTRY
)
AddBlockOps = Gauge(
 'AddBlockOps',
 'AddBlockOps',
 registry=REGISTRY
)
GetAdditionalDatanodeOps = Gauge(
 'GetAdditionalDatanodeOps',
 'GetAdditionalDatanodeOps',
 registry=REGISTRY
)
CreateSymlinkOps = Gauge(
 'CreateSymlinkOps',
 'CreateSymlinkOps',
 registry=REGISTRY
)
GetLinkTargetOps = Gauge(
 'GetLinkTargetOps',
 'GetLinkTargetOps',
 registry=REGISTRY
)
FilesInGetListingOps = Gauge(
 'FilesInGetListingOps',
 'FilesInGetListingOps',
 registry=REGISTRY
)
AllowSnapshotOps = Gauge(
 'AllowSnapshotOps',
 'AllowSnapshotOps',
 registry=REGISTRY
)
DisallowSnapshotOps = Gauge(
 'DisallowSnapshotOps',
 'DisallowSnapshotOps',
 registry=REGISTRY
)
CreateSnapshotOps = Gauge(
 'CreateSnapshotOps',
 'CreateSnapshotOps',
 registry=REGISTRY
)
DeleteSnapshotOps = Gauge(
 'DeleteSnapshotOps',
 'DeleteSnapshotOps',
 registry=REGISTRY
)
RenameSnapshotOps = Gauge(
 'RenameSnapshotOps',
 'RenameSnapshotOps',
 registry=REGISTRY
)
ListSnapshottableDirOps = Gauge(
 'ListSnapshottableDirOps',
 'ListSnapshottableDirOps',
 registry=REGISTRY
)
BlockReceivedAndDeletedOps = Gauge(
 'BlockReceivedAndDeletedOps',
 'BlockReceivedAndDeletedOps',
 registry=REGISTRY
)
StorageBlockReportOps = Gauge(
 'StorageBlockReportOps',
 'StorageBlockReportOps',
 registry=REGISTRY
)
TransactionsNumOps = Gauge(
 'TransactionsNumOps',
 'TransactionsNumOps',
 registry=REGISTRY
)
TransactionsAvgTime = Gauge(
 'TransactionsAvgTime',
 'TransactionsAvgTime',
 registry=REGISTRY
)
SyncsNumOps = Gauge(
 'SyncsNumOps',
 'SyncsNumOps',
 registry=REGISTRY
)
SyncsAvgTime = Gauge(
 'SyncsAvgTime',
 'SyncsAvgTime',
 registry=REGISTRY
)
TransactionsBatchedInSync = Gauge(
 'TransactionsBatchedInSync',
 'TransactionsBatchedInSync',
 registry=REGISTRY
)

BlockReportAvgTime = Gauge(
 'BlockReportAvgTime',
 'BlockReportAvgTime',
 registry=REGISTRY
)
CacheReportNumOps = Gauge(
 'CacheReportNumOps',
 'CacheReportNumOps',
 registry=REGISTRY
)
CacheReportAvgTime = Gauge(
 'CacheReportAvgTime',
 'CacheReportAvgTime',
 registry=REGISTRY
)
SafeModeTime = Gauge(
 'SafeModeTime',
 'SafeModeTime',
 registry=REGISTRY
)
FsImageLoadTime = Gauge(
 'FsImageLoadTime',
 'FsImageLoadTime',
 registry=REGISTRY
)
GetEditNumOps = Gauge(
 'GetEditNumOps',
 'GetEditNumOps',
 registry=REGISTRY
)
GetEditAvgTime = Gauge(
 'GetEditAvgTime',
 'GetEditAvgTime',
 registry=REGISTRY
)
GetImageNumOps = Gauge(
 'GetImageNumOps',
 'GetImageNumOps',
 registry=REGISTRY
)
GetImageAvgTime = Gauge(
 'GetImageAvgTime',
 'GetImageAvgTime',
 registry=REGISTRY
)
PutImageNumOps = Gauge(
 'PutImageNumOps',
 'PutImageNumOps',
 registry=REGISTRY
)
PutImageAvgTime = Gauge(
 'PutImageAvgTime',
 'PutImageAvgTime',
 registry=REGISTRY
)
CollectionCount = Gauge(
 'CollectionCount',
 'CollectionCount',
 registry=REGISTRY
)
CollectionTime = Gauge(
 'CollectionTime',
 'CollectionTime',
 registry=REGISTRY
)

isActive = Gauge(
 'isActive',
 'isActive',
 registry=REGISTRY
)

isStandby = Gauge(
 'isStandby',
 'isStandby',
 registry=REGISTRY
)

Used = Gauge(
 'Used',
 'Used',
 registry=REGISTRY
)
Free = Gauge(
 'Free',
 'Free',
 registry=REGISTRY
)

NonDfsUsedSpace = Gauge(
 'NonDfsUsedSpace',
 'NonDfsUsedSpace',
 registry=REGISTRY
)
PercentUsed = Gauge(
 'PercentUsed',
 'PercentUsed',
 registry=REGISTRY
)
BlockPoolUsedSpace = Gauge(
 'BlockPoolUsedSpace',
 'BlockPoolUsedSpace',
 registry=REGISTRY
)
PercentBlockPoolUsed = Gauge(
 'PercentBlockPoolUsed',
 'PercentBlockPoolUsed',
 registry=REGISTRY
)
PercentRemaining = Gauge(
 'PercentRemaining',
 'PercentRemaining',
 registry=REGISTRY
)
CacheCapacity = Gauge(
 'CacheCapacity',
 'CacheCapacity',
 registry=REGISTRY
)
CacheUsed = Gauge(
 'CacheUsed',
 'CacheUsed',
 registry=REGISTRY
)
TotalBlocks = Gauge(
 'TotalBlocks',
 'TotalBlocks',
 registry=REGISTRY
)

NumberOfMissingBlocks = Gauge(
 'NumberOfMissingBlocks',
 'NumberOfMissingBlocks',
 registry=REGISTRY
)
Threads = Gauge(
 'Threads',
 'Threads',
 registry=REGISTRY
)
SnapshottableDirectories = Gauge(
 'SnapshottableDirectories',
 'SnapshottableDirectories',
 registry=REGISTRY
)

CapacityUsed = Gauge(
 'CapacityUsed',
 'CapacityUsed',
 registry=REGISTRY
)
CapacityRemaining = Gauge(
 'CapacityRemaining',
 'CapacityRemaining',
 registry=REGISTRY
)
TotalLoad = Gauge(
 'TotalLoad',
 'TotalLoad',
 registry=REGISTRY
)
BlocksTotal = Gauge(
 'BlocksTotal',
 'BlocksTotal',
 registry=REGISTRY
)
MaxObjects = Gauge(
 'MaxObjects',
 'MaxObjects',
 registry=REGISTRY
)

PendingReplicationBlocks = Gauge(
 'PendingReplicationBlocks',
 'PendingReplicationBlocks',
 registry=REGISTRY
)
UnderReplicatedBlocks = Gauge(
 'UnderReplicatedBlocks',
 'UnderReplicatedBlocks',
 registry=REGISTRY
)


BlockDeletionStartTime = Gauge(
 'BlockDeletionStartTime',
 'BlockDeletionStartTime',
 registry=REGISTRY
)

NumLiveDataNodes = Gauge(
 'NumLiveDataNodes',
 'NumLiveDataNodes',
 registry=REGISTRY
)
NumDeadDataNodes = Gauge(
 'NumDeadDataNodes',
 'NumDeadDataNodes',
 registry=REGISTRY
)
NumDecomLiveDataNodes = Gauge(
 'NumDecomLiveDataNodes',
 'NumDecomLiveDataNodes',
 registry=REGISTRY
)
NumDecomDeadDataNodes = Gauge(
 'NumDecomDeadDataNodes',
 'NumDecomDeadDataNodes',
 registry=REGISTRY
)
NumDecommissioningDataNodes = Gauge(
 'NumDecommissioningDataNodes',
 'NumDecommissioningDataNodes',
 registry=REGISTRY
)
NumStaleDataNodes = Gauge(
 'NumStaleDataNodes',
 'NumStaleDataNodes',
 registry=REGISTRY
)
NumStaleStorages = Gauge(
 'NumStaleStorages',
 'NumStaleStorages',
 registry=REGISTRY
)
ReceivedBytes = Gauge(
 'ReceivedBytes',
 'ReceivedBytes',
 registry=REGISTRY
)
SentBytes = Gauge(
 'SentBytes',
 'SentBytes',
 registry=REGISTRY
)
RpcQueueTimeNumOps = Gauge(
 'RpcQueueTimeNumOps',
 'RpcQueueTimeNumOps',
 registry=REGISTRY
)
RpcQueueTimeAvgTime = Gauge(
 'RpcQueueTimeAvgTime',
 'RpcQueueTimeAvgTime',
 registry=REGISTRY
)
RpcProcessingTimeNumOps = Gauge(
 'RpcProcessingTimeNumOps',
 'RpcProcessingTimeNumOps',
 registry=REGISTRY
)
RpcProcessingTimeAvgTime = Gauge(
 'RpcProcessingTimeAvgTime',
 'RpcProcessingTimeAvgTime',
 registry=REGISTRY
)
RpcAuthenticationFailures = Gauge(
 'RpcAuthenticationFailures',
 'RpcAuthenticationFailures',
 registry=REGISTRY
)
RpcAuthenticationSuccesses = Gauge(
 'RpcAuthenticationSuccesses',
 'RpcAuthenticationSuccesses',
 registry=REGISTRY
)
RpcAuthorizationFailures = Gauge(
 'RpcAuthorizationFailures',
 'RpcAuthorizationFailures',
 registry=REGISTRY
)
RpcAuthorizationSuccesses = Gauge(
 'RpcAuthorizationSuccesses',
 'RpcAuthorizationSuccesses',
 registry=REGISTRY
)
NumOpenConnections = Gauge(
 'NumOpenConnections',
 'NumOpenConnections',
 registry=REGISTRY
)
CallQueueLength = Gauge(
 'CallQueueLength',
 'CallQueueLength',
 registry=REGISTRY
)




c = Counter('my_requests_total', 'HTTP Failures', ['method', 'endpoint'],registry=REGISTRY)

#Strinfo = Ha()


@app.route('/metrics')
def r_value():
    global StandbyOrActive
    for per_metrics in dict:
        #print(per_metrics['name'])

        if per_metrics['name'] == "Hadoop:service=NameNode,name=NameNodeStatus":
            if per_metrics['State']=="active":
                    StandbyOrActive=1
                    isActive.set(StandbyOrActive)
            if per_metrics['State']=="standby":
                    StandbyOrActive=0
                    isActive.set(StandbyOrActive)

        if per_metrics['name'] == "Hadoop:service=NameNode,name=JvmMetrics":
         MemNonHeapUsedM.set(per_metrics['MemNonHeapUsedM'])
         MemNonHeapCommittedM.set(per_metrics['MemNonHeapCommittedM'])
         MemNonHeapMaxM.set(per_metrics['MemNonHeapMaxM'])
         MemHeapUsedM.set(per_metrics['MemHeapUsedM'])
         MemHeapCommittedM.set(per_metrics['MemHeapCommittedM'])
         MemHeapMaxM.set(per_metrics['MemHeapMaxM'])
         MemMaxM.set(per_metrics['MemMaxM'])
         GcCount.set(per_metrics['GcCount'])
         GcTimeMillis.set(per_metrics['GcTimeMillis'])
         GcNumWarnThresholdExceeded.set(per_metrics['GcNumWarnThresholdExceeded'])
         GcNumInfoThresholdExceeded.set(per_metrics['GcNumInfoThresholdExceeded'])
         GcTotalExtraSleepTime.set(per_metrics['GcTotalExtraSleepTime'])
         ThreadsNew.set(per_metrics['ThreadsNew'])
         ThreadsRunnable.set(per_metrics['ThreadsRunnable'])
         ThreadsBlocked.set(per_metrics['ThreadsBlocked'])
         ThreadsWaiting.set(per_metrics['ThreadsWaiting'])
         ThreadsTimedWaiting.set(per_metrics['ThreadsTimedWaiting'])
         ThreadsTerminated.set(per_metrics['ThreadsTerminated'])
         LogFatal.set(per_metrics['LogFatal'])
         LogError.set(per_metrics['LogError'])
         LogWarn.set(per_metrics['LogWarn'])
         LogInfo.set(per_metrics['LogInfo'])

        if per_metrics['name'] == "java.lang:type=Runtime":
         Uptime.set(per_metrics['Uptime'])
         StartTime.set(per_metrics['StartTime'])

        if per_metrics['name'] == "Hadoop:service=NameNode,name=StartupProgress":
         ElapsedTime.set(per_metrics['ElapsedTime'])
         PercentComplete.set(per_metrics['PercentComplete'])
         LoadingFsImageCount.set(per_metrics['LoadingFsImageCount'])
         LoadingFsImageElapsedTime.set(per_metrics['LoadingFsImageElapsedTime'])
         LoadingFsImageTotal.set(per_metrics['LoadingFsImageTotal'])
         LoadingFsImagePercentComplete.set(per_metrics['LoadingFsImagePercentComplete'])
         LoadingEditsCount.set(per_metrics['LoadingEditsCount'])
         LoadingEditsElapsedTime.set(per_metrics['LoadingEditsElapsedTime'])
         LoadingEditsTotal.set(per_metrics['LoadingEditsTotal'])
         LoadingEditsPercentComplete.set(per_metrics['LoadingEditsPercentComplete'])
         SavingCheckpointCount.set(per_metrics['SavingCheckpointCount'])
         SavingCheckpointElapsedTime.set(per_metrics['SavingCheckpointElapsedTime'])
         SavingCheckpointTotal.set(per_metrics['SavingCheckpointTotal'])
         SavingCheckpointPercentComplete.set(per_metrics['SavingCheckpointPercentComplete'])
         SafeModeCount.set(per_metrics['SafeModeCount'])
         SafeModeElapsedTime.set(per_metrics['SafeModeElapsedTime'])
         SafeModeTotal.set(per_metrics['SafeModeTotal'])
         SafeModePercentComplete.set(per_metrics['SafeModePercentComplete'])

        if per_metrics['name'] == "java.lang:type=Threading":
         PeakThreadCount.set(per_metrics['PeakThreadCount'])
         DaemonThreadCount.set(per_metrics['DaemonThreadCount'])
         TotalStartedThreadCount.set(per_metrics['TotalStartedThreadCount'])
         ThreadCpuTimeSupported.set(per_metrics['ThreadCpuTimeSupported'])
         CurrentThreadCpuTime.set(per_metrics['CurrentThreadCpuTime'])
         CurrentThreadUserTime.set(per_metrics['CurrentThreadUserTime'])
         ThreadCount.set(per_metrics['ThreadCount'])

        if per_metrics['name'] == "java.lang:type=OperatingSystem":
         OpenFileDescriptorCount.set(per_metrics['OpenFileDescriptorCount'])
         MaxFileDescriptorCount.set(per_metrics['MaxFileDescriptorCount'])
         CommittedVirtualMemorySize.set(per_metrics['CommittedVirtualMemorySize'])
         TotalSwapSpaceSize.set(per_metrics['TotalSwapSpaceSize'])
         FreeSwapSpaceSize.set(per_metrics['FreeSwapSpaceSize'])
         ProcessCpuTime.set(per_metrics['ProcessCpuTime'])
         FreePhysicalMemorySize.set(per_metrics['FreePhysicalMemorySize'])
         TotalPhysicalMemorySize.set(per_metrics['TotalPhysicalMemorySize'])
         SystemCpuLoad.set(per_metrics['SystemCpuLoad'])
         ProcessCpuLoad.set(per_metrics['ProcessCpuLoad'])
         AvailableProcessors.set(per_metrics['AvailableProcessors'])

        if per_metrics['name'] == "Hadoop:service=NameNode,name=FSNamesystem":
         MissingBlocks.set(per_metrics['MissingBlocks'])
         ExpiredHeartbeats.set(per_metrics['ExpiredHeartbeats'])
         TransactionsSinceLastCheckpoint.set(per_metrics['TransactionsSinceLastCheckpoint'])
         TransactionsSinceLastLogRoll.set(per_metrics['TransactionsSinceLastLogRoll'])
         LastWrittenTransactionId.set(per_metrics['LastWrittenTransactionId'])
         LastCheckpointTime.set(per_metrics['LastCheckpointTime'])
         CapacityTotal.set(per_metrics['CapacityTotal'])
         CapacityTotalGB.set(per_metrics['CapacityTotalGB'])
         CapacityUsed.set(per_metrics['CapacityUsed'])
         CapacityUsedGB.set(per_metrics['CapacityUsedGB'])
         CapacityRemaining.set(per_metrics['CapacityRemaining'])
         CapacityRemainingGB.set(per_metrics['CapacityRemainingGB'])
         CapacityUsedNonDFS.set(per_metrics['CapacityUsedNonDFS'])
         TotalLoad.set(per_metrics['TotalLoad'])
         SnapshottableDirectories.set(per_metrics['SnapshottableDirectories'])
         Snapshots.set(per_metrics['Snapshots'])
         BlocksTotal.set(per_metrics['BlocksTotal'])
         FilesTotal.set(per_metrics['FilesTotal'])
         PendingReplicationBlocks.set(per_metrics['PendingReplicationBlocks'])
         UnderReplicatedBlocks.set(per_metrics['UnderReplicatedBlocks'])
         CorruptBlocks.set(per_metrics['CorruptBlocks'])
         ScheduledReplicationBlocks.set(per_metrics['ScheduledReplicationBlocks'])
         PendingDeletionBlocks.set(per_metrics['PendingDeletionBlocks'])
         ExcessBlocks.set(per_metrics['ExcessBlocks'])
         PostponedMisreplicatedBlocks.set(per_metrics['PostponedMisreplicatedBlocks'])
         PendingDataNodeMessageCount.set(per_metrics['PendingDataNodeMessageCount'])
         MillisSinceLastLoadedEdits.set(per_metrics['MillisSinceLastLoadedEdits'])
         BlockCapacity.set(per_metrics['BlockCapacity'])
         StaleDataNodes.set(per_metrics['StaleDataNodes'])
         TotalFiles.set(per_metrics['TotalFiles'])

        if per_metrics['name'] == "Hadoop:service=NameNode,name=RpcDetailedActivityForPort8020":
         VersionRequestNumOps.set(per_metrics['VersionRequestNumOps'])
         VersionRequestAvgTime.set(per_metrics['VersionRequestAvgTime'])
         RegisterDatanodeNumOps.set(per_metrics['RegisterDatanodeNumOps'])
         RegisterDatanodeAvgTime.set(per_metrics['RegisterDatanodeAvgTime'])
         SendHeartbeatNumOps.set(per_metrics['SendHeartbeatNumOps'])
         SendHeartbeatAvgTime.set(per_metrics['SendHeartbeatAvgTime'])
         BlockReportNumOps.set(per_metrics['BlockReportNumOps'])
         BlockReportAvgTime.set(per_metrics['BlockReportAvgTime'])
         GetServiceStatusNumOps.set(per_metrics['GetServiceStatusNumOps'])
         GetServiceStatusAvgTime.set(per_metrics['GetServiceStatusAvgTime'])
         MonitorHealthNumOps.set(per_metrics['MonitorHealthNumOps'])
         MonitorHealthAvgTime.set(per_metrics['MonitorHealthAvgTime'])
         TransitionToActiveNumOps.set(per_metrics['TransitionToActiveNumOps'])
         TransitionToActiveAvgTime.set(per_metrics['TransitionToActiveAvgTime'])
         RollEditLogNumOps.set(per_metrics['RollEditLogNumOps'])
         RollEditLogAvgTime.set(per_metrics['RollEditLogAvgTime'])
         #TransitionToStandbyNumOps.set(per_metrics['TransitionToStandbyNumOps'])
         #TransitionToStandbyAvgTime.set(per_metrics['TransitionToStandbyAvgTime'])


        if per_metrics['name'] == "Hadoop:service=NameNode,name=NameNodeActivity":
         CreateFileOps.set(per_metrics['CreateFileOps'])
         FilesCreated.set(per_metrics['FilesCreated'])
         FilesAppended.set(per_metrics['FilesAppended'])
         GetBlockLocations.set(per_metrics['GetBlockLocations'])
         FilesRenamed.set(per_metrics['FilesRenamed'])
         GetListingOps.set(per_metrics['GetListingOps'])
         DeleteFileOps.set(per_metrics['DeleteFileOps'])
         FilesDeleted.set(per_metrics['FilesDeleted'])
         FileInfoOps.set(per_metrics['FileInfoOps'])
         AddBlockOps.set(per_metrics['AddBlockOps'])
         GetAdditionalDatanodeOps.set(per_metrics['GetAdditionalDatanodeOps'])
         CreateSymlinkOps.set(per_metrics['CreateSymlinkOps'])
         GetLinkTargetOps.set(per_metrics['GetLinkTargetOps'])
         FilesInGetListingOps.set(per_metrics['FilesInGetListingOps'])
         AllowSnapshotOps.set(per_metrics['AllowSnapshotOps'])
         DisallowSnapshotOps.set(per_metrics['DisallowSnapshotOps'])
         CreateSnapshotOps.set(per_metrics['CreateSnapshotOps'])
         DeleteSnapshotOps.set(per_metrics['DeleteSnapshotOps'])
         RenameSnapshotOps.set(per_metrics['RenameSnapshotOps'])
         ListSnapshottableDirOps.set(per_metrics['ListSnapshottableDirOps'])
         # SnapshotDiffReportOps.set(per_metrics['SnapshotDiffReportOps'])
         BlockReceivedAndDeletedOps.set(per_metrics['BlockReceivedAndDeletedOps'])
         StorageBlockReportOps.set(per_metrics['StorageBlockReportOps'])
         TransactionsNumOps.set(per_metrics['TransactionsNumOps'])
         TransactionsAvgTime.set(per_metrics['TransactionsAvgTime'])
         SyncsNumOps.set(per_metrics['SyncsNumOps'])
         SyncsAvgTime.set(per_metrics['SyncsAvgTime'])
         TransactionsBatchedInSync.set(per_metrics['TransactionsBatchedInSync'])
         CacheReportNumOps.set(per_metrics['CacheReportNumOps'])
         CacheReportAvgTime.set(per_metrics['CacheReportAvgTime'])
         SafeModeTime.set(per_metrics['SafeModeTime'])
         FsImageLoadTime.set(per_metrics['FsImageLoadTime'])
         GetEditNumOps.set(per_metrics['GetEditNumOps'])
         GetEditAvgTime.set(per_metrics['GetEditAvgTime'])
         GetImageNumOps.set(per_metrics['GetImageNumOps'])
         GetImageAvgTime.set(per_metrics['GetImageAvgTime'])
         PutImageNumOps.set(per_metrics['PutImageNumOps'])
         PutImageAvgTime.set(per_metrics['PutImageAvgTime'])

        if per_metrics['name'] == "java.lang:type=GarbageCollector,name=PS":
         CollectionCount.set(per_metrics['CollectionCount'])
         CollectionTime.set(per_metrics['CollectionTime'])
        if per_metrics['name'] == "Hadoop:service=NameNode,name=NameNodeInfo":
         Used.set(per_metrics['Used'])
         Free.set(per_metrics['Free'])
         # Safemode.set(per_metrics['Safemode'])
         NonDfsUsedSpace.set(per_metrics['NonDfsUsedSpace'])
         PercentUsed.set(per_metrics['PercentUsed'])
         BlockPoolUsedSpace.set(per_metrics['BlockPoolUsedSpace'])
         PercentBlockPoolUsed.set(per_metrics['PercentBlockPoolUsed'])
         PercentRemaining.set(per_metrics['PercentRemaining'])
         CacheCapacity.set(per_metrics['CacheCapacity'])
         CacheUsed.set(per_metrics['CacheUsed'])
         TotalBlocks.set(per_metrics['TotalBlocks'])
         # TotalFiles.set(per_metrics['TotalFiles'])
         NumberOfMissingBlocks.set(per_metrics['NumberOfMissingBlocks'])
         Threads.set(per_metrics['Threads'])

        if per_metrics['name'] == "Hadoop:service=NameNode,name=FSNamesystemState":
         # CapacityUsed.set(per_metrics['CapacityUsed'])
         # CapacityRemaining.set(per_metrics['CapacityRemaining'])
         # TotalLoad.set(per_metrics['TotalLoad'])
         # BlocksTotal.set(per_metrics['BlocksTotal'])
         MaxObjects.set(per_metrics['MaxObjects'])
         # FilesTotal.set(per_metrics['FilesTotal'])
         # PendingReplicationBlocks.set(per_metrics['PendingReplicationBlocks'])
         # UnderReplicatedBlocks.set(per_metrics['UnderReplicatedBlocks'])
         # ScheduledReplicationBlocks.set(per_metrics['ScheduledReplicationBlocks'])
         # PendingDeletionBlocks.set(per_metrics['PendingDeletionBlocks'])
         BlockDeletionStartTime.set(per_metrics['BlockDeletionStartTime'])
         # FSState.set(per_metrics['FSState'])
         NumLiveDataNodes.set(per_metrics['NumLiveDataNodes'])
         NumDeadDataNodes.set(per_metrics['NumDeadDataNodes'])
         NumDecomLiveDataNodes.set(per_metrics['NumDecomLiveDataNodes'])
         NumDecomDeadDataNodes.set(per_metrics['NumDecomDeadDataNodes'])
         NumDecommissioningDataNodes.set(per_metrics['NumDecommissioningDataNodes'])
         NumStaleDataNodes.set(per_metrics['NumStaleDataNodes'])
         NumStaleStorages.set(per_metrics['NumStaleStorages'])

        if per_metrics['name'] == "Hadoop:service=NameNode,name=RpcActivityForPort8020":
         ReceivedBytes.set(per_metrics['ReceivedBytes'])
         SentBytes.set(per_metrics['SentBytes'])
         RpcQueueTimeNumOps.set(per_metrics['RpcQueueTimeNumOps'])
         RpcQueueTimeAvgTime.set(per_metrics['RpcQueueTimeAvgTime'])
         RpcProcessingTimeNumOps.set(per_metrics['RpcProcessingTimeNumOps'])
         RpcProcessingTimeAvgTime.set(per_metrics['RpcProcessingTimeAvgTime'])
         RpcAuthenticationFailures.set(per_metrics['RpcAuthenticationFailures'])
         RpcAuthenticationSuccesses.set(per_metrics['RpcAuthenticationSuccesses'])
         RpcAuthorizationFailures.set(per_metrics['RpcAuthorizationFailures'])
         RpcAuthorizationSuccesses.set(per_metrics['RpcAuthorizationSuccesses'])
         NumOpenConnections.set(per_metrics['NumOpenConnections'])
         CallQueueLength.set(per_metrics['CallQueueLength'])


    c.labels('test', '1').inc()
    c.labels('post', '/submit').inc()
    c.labels(activeurl, '1')
    return Response(prometheus_client.generate_latest(REGISTRY),
                    mimetype="text/plain")


@app.route('/')
def index():
     return "<html>" \
        "<head><title>Namenode Exporter</title></head>" \
        "<body>" \
        "<h1>Namenode "+ activeurl +" Exporter</h1>" \
        "<p><a href="  +('/metrics') + ">Metrics</a></p></body>" \
                                       "</html>"


if __name__ == "__main__":
    #global StandbyOrActive
    base_info=clusterBaseinfo()
    #yarn集群主备节点
    namenode_ip = ["192.168.10.10:50070","192.168.10.11:50070"]
    #对active状态的节点抓取metrics数据
    activeurl=base_info.getactivenode(namenode_ip)
    # html = urllib.request.urlopen(rmurl)
    # hjson = json.loads(html.read())
    #for nnurl in namenode_ip:
    html = requests.get('http://' + activeurl + '/jmx')
    hjson = json.loads(html.text)
    dict = hjson['beans']
    app.run(host='localhost', port=9099, debug=True)

    #html = urllib.request.urlopen(r'http://192.168.10.10:50070/jmx')
    #hjson = json.loads(html.read())
